//
//  STGetCityArea.m
//  YiYueStore
//
//  Created by zhouzhouzhou on 2018/10/25.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STGetCityArea.h"
#import "STFirstControl.h"

@implementation STGetCityArea

static STGetCityArea* obj;
+(instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        obj = [[STGetCityArea alloc] init];
    });
    return obj;
}

-(void)getSelfCityId:(STResultObjectBlock)complection{
    NSString * cityName = [STFirstControl sharedInstance].selfRegeocode.city;
    __block long cityId;
    if (!cityName.length) {
        cityName = @"深圳";
    }
    WeakSelf
    if (!self.arrCitys.count) {
        [STMainPageService getAllCityInfo:^(id result, NSError *error) {
            if (error) {
                [ZYHCommonMethod showError:error];
            }
            NSArray * arrModel = [NSArray modelArrayWithClass:[STCityInfoModel class] json:result];
            weakSelf.arrCitys = arrModel;
            for (STCityInfoModel * provinceModel in weakSelf.arrCitys) {
                for (STCityInfoModel * urbanModel in provinceModel.areaList) {
                    if ([urbanModel.name containsString:cityName]) {
                        cityId = urbanModel.parentId;
                        
                        selfCityId =[NSString stringWithFormat:@"%ld",cityId];
                        weakSelf.arrCitys = urbanModel.areaList;
                        complection(urbanModel.areaList,nil);
                        return;
                    }
                }
            }
        }];
    }else{
      complection(weakSelf.arrCitys,nil);
    }
        
}

-(void)getSeflAreas:(STResultObjectBlock)complection{
    [STMainPageService getHairerInfo:[selfCityId longValue] completion:^(id result, NSError *error) {
        if (error) {
            [ZYHCommonMethod showError:error];
        }
        NSArray * arrModel = [NSArray modelArrayWithClass:[STCityInfoModel class] json:result];
        complection(arrModel,error);
    }];
}


@end
